package com.db.morphia;

import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;

import com.mongodb.MongoClient;

public class MorphiaFactory {

	public static Datastore create(String ip, int port, String db) {
		// 连接到 mongodb 服务
		MongoClient mongoClient = new MongoClient(ip, port);
		// 连接到数据库,如果指定的数据库不存在，mongo会自动创建数据库
		Morphia morphia = new Morphia();
		Datastore datastore = morphia.createDatastore(mongoClient, db);
		System.out.println(String.format("Connect to database [%s] successfully", datastore.getDB()));
		return datastore;
	}
	
	public static Datastore create(String ip, int port, String db, String entityPackage) {
		// 连接到 mongodb 服务
		MongoClient mongoClient = new MongoClient(ip, port);
		// 连接到数据库,如果指定的数据库不存在，mongo会自动创建数据库
		Morphia morphia = new Morphia();
		morphia.mapPackage(entityPackage);
		Datastore datastore = morphia.createDatastore(mongoClient, db);
		System.out.println(String.format("Connect to database [%s] successfully", datastore.getDB()));
		return datastore;
	}

}
